package xhwl.hmo.mall.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.utils.SpUtil;

public class GuideActivity extends Activity implements OnPageChangeListener {
	// 滑动页面
	ViewPager viewpager;
	// 视图链表，viewpager的数据源
	List<View> mylist = new ArrayList<View>();
	private int currentItem = 0;
	public GestureDetector mGestureDetector;
	private int flaggingWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		slipToMain();
		// 获取分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		flaggingWidth = dm.widthPixels / 8;
		viewInit();
	}

	public void viewInit() {

		viewpager = (ViewPager) findViewById(R.id.myviewpager);
		// 引用布局

		mylist.add(getLayoutInflater().inflate(R.layout.guide_one, null));
		mylist.add(getLayoutInflater().inflate(R.layout.guide_two, null));
		mylist.add(getLayoutInflater().inflate(R.layout.guide_three, null));
		// 初始化适配器
		ViewPagerAdapter myadapter = new ViewPagerAdapter(this, mylist);
		// 加载适配器
		viewpager.setAdapter(myadapter);
		viewpager.setOnPageChangeListener(this);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		currentItem = arg0;

	}

	private void slipToMain() {
		mGestureDetector = new GestureDetector(this,
				new GestureDetector.SimpleOnGestureListener() {

					@Override
					public boolean onFling(MotionEvent e1, MotionEvent e2,
										   float velocityX, float velocityY) {
						if (currentItem == 2) {
							if ((e1.getRawX() - e2.getRawX()) >= flaggingWidth) {
								SpUtil.putBoolean(SpUtil.SHOW_GUIDE, false);
								Intent intent = new Intent(
										GuideActivity.this,
										MainActivity.class);
								startActivity(intent);
								finish();
								return true;
							}
						}
						return false;
					}

				});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (mGestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}

	public class ViewPagerAdapter extends PagerAdapter {

		List<View> mylist;
		Activity myactivity;

		public ViewPagerAdapter(Activity outactivity, List<View> outlist) {
			mylist = outlist;
			myactivity = outactivity;
		}

		@Override
		public int getCount() {

			return mylist.size();

		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1;

		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(mylist.get(position));
			System.out.println("now delete");
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mylist.get(position));

//			if (position == mylist.size()) {
//
//				Editor editor = myactivity.getSharedPreferences("info",
//						Context.MODE_PRIVATE).edit();
//				editor.putBoolean("guide", false);
//				editor.commit();
//				Intent intent = new Intent(myactivity, SplachActivity.class);
//				myactivity.startActivity(intent);
//
//			}

			return mylist.get(position);

		}
	}
}
